package com.yupi.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.usercenter.feign.client.LmdmClientHelper;
import com.yupi.usercenter.mapper.MonitorWoLogMapper;
import com.yupi.usercenter.model.entity.MonitorWoLog;
import com.yupi.usercenter.model.vo.MonitorWoLogDetailVO;
import com.yupi.usercenter.model.vo.MonitorWoLogVO;
import com.yupi.usercenter.model.vo.ProblemExpressWoExcelVO;
import com.yupi.usercenter.service.IMonitorWoLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 云路供应链科技有限公司 版权所有 © Copyright 2020
 * <p>
 * 监控工单记录 服务实现类
 *
 * @author jiweiyu
 * @version 1.0.0
 * @date Created in 2021-03-05
 */
@Slf4j
@Service
public class MonitorWoLogServiceImpl extends ServiceImpl<MonitorWoLogMapper, MonitorWoLog> implements IMonitorWoLogService {

    @Autowired
    private LmdmClientHelper lmdmClientHelper;

    @Resource
    private MonitorWoLogMapper monitorWoLogMapper;

    @Override
    public MonitorWoLogDetailVO detail(long id) {
        return null;
    }

    @Override
    public Boolean add(MonitorWoLog dto) {
        dto.setCreateTime(LocalDateTime.now());
        dto.setCreateBy(dto.getCreateBy());
        dto.setCreateByName(dto.getCreateByName());
        return this.save(dto);
    }

    @Override
    public List<MonitorWoLogVO> getProblemExpressWoLogList(Long workOrderId) {
        List<MonitorWoLog> monitorWoLogVOS = this.list(new LambdaQueryWrapper<MonitorWoLog>()
                .select(MonitorWoLog::getId,
                        MonitorWoLog::getWorkOrderId, MonitorWoLog::getToNetworkId,
                        MonitorWoLog::getToNetworkCode, MonitorWoLog::getToNetworkName,
                        MonitorWoLog::getFromNetworkId, MonitorWoLog::getFromNetworkCode,
                        MonitorWoLog::getFromNetworkName, MonitorWoLog::getFromNetworkType,
                        MonitorWoLog::getOperationType, MonitorWoLog::getProblemTypeCode,
                        MonitorWoLog::getProblemTypeName, MonitorWoLog::getProblemTypeSubjectCode,
                        MonitorWoLog::getProblemTypeSubjectName, MonitorWoLog::getPenaltyAmount,
                        MonitorWoLog::getAwardAmount, MonitorWoLog::getSubsidyAmount,
                        MonitorWoLog::getHandlingFee, MonitorWoLog::getProblemPieceAttachment,
                        MonitorWoLog::getProofAttachment, MonitorWoLog::getProcessorOpinions,
                        MonitorWoLog::getContactInformation, MonitorWoLog::getProcessorName, MonitorWoLog::getId,
                        MonitorWoLog::getCreateBy, MonitorWoLog::getCreateByName, MonitorWoLog::getCreateTime
                )
                .eq(MonitorWoLog::getWorkOrderId, workOrderId)
                .orderByDesc(MonitorWoLog::getCreateTime));

        return generateMonitorWoLogVOs(monitorWoLogVOS);
    }

    private List<MonitorWoLogVO> generateMonitorWoLogVOs(List<MonitorWoLog> monitorWoLogs ) {
        List<MonitorWoLogVO> monitorWoLogVOList = new ArrayList<>();
        for (MonitorWoLog monitorWoLog : monitorWoLogs) {
            MonitorWoLogVO monitorWoLogVO = new MonitorWoLogVO();
            monitorWoLogVO.setId(monitorWoLog.getId());
            monitorWoLogVO.setWorkOrderId(monitorWoLog.getWorkOrderId());
            monitorWoLogVO.setToNetworkId(monitorWoLog.getToNetworkId());
            monitorWoLogVO.setToNetworkCode(monitorWoLog.getToNetworkCode());
            monitorWoLogVO.setToNetworkName(monitorWoLog.getToNetworkName());
            monitorWoLogVO.setFromNetworkId(monitorWoLog.getFromNetworkId());
            monitorWoLogVO.setFromNetworkCode(monitorWoLog.getFromNetworkCode());
            monitorWoLogVO.setFromNetworkName(monitorWoLog.getFromNetworkName());
            monitorWoLogVO.setOperationType(monitorWoLog.getOperationType());
            monitorWoLogVO.setPenaltyAmount(monitorWoLog.getPenaltyAmount());
            monitorWoLogVO.setAwardAmount(monitorWoLog.getAwardAmount());
            monitorWoLogVO.setSubsidyAmount(monitorWoLog.getSubsidyAmount());
            monitorWoLogVO.setHandlingFee(monitorWoLog.getHandlingFee());

            monitorWoLogVO.setProcessorOpinions(monitorWoLog.getProcessorOpinions());
            monitorWoLogVO.setContactInformation(monitorWoLog.getContactInformation());
            monitorWoLogVO.setProcessorName(monitorWoLog.getProcessorName());
            monitorWoLogVO.setCreateTime(monitorWoLog.getCreateTime());
            monitorWoLogVOList.add(monitorWoLogVO);
        }
        return monitorWoLogVOList;
    }

    @Override
    public List<MonitorWoLog> findBatchByProblemExpressWoIds(List<Long> ids) {
        return this.list(new LambdaQueryWrapper<MonitorWoLog>().
                select(MonitorWoLog::getWorkOrderId,MonitorWoLog::getFromNetworkType,
                        MonitorWoLog::getFromNetworkName,MonitorWoLog::getProcessorName)
                .in(MonitorWoLog::getWorkOrderId,ids));
    }

    @Override
    public MonitorWoLog findLastLogByProblemExpressId(Long id) {
        return null;
    }

    @Override
    public List<ProblemExpressWoExcelVO> findLastProcessNodeByWorkOrderIdList(List<Long> ids) {
        return null;
    }

    @Override
    public void deleteBilledData(List<Integer> workIdList) {
        monitorWoLogMapper.deleteBilledData(workIdList);
    }

    @Override
    public String getCompletePhone(Long id) {
        MonitorWoLog monitorWoLog = baseMapper.selectOne(new QueryWrapper<MonitorWoLog>().select("contact_information").eq("id", id));
        if(ObjectUtils.isNotEmpty(monitorWoLog)){
            return monitorWoLog.getContactInformation();
        }
        return null;
    }

}
